import type { RouteRecordRaw } from "vue-router"
import { createRouter } from "vue-router"
import { routerConfig } from "@/router/config"
import { registerNavigationGuard } from "@/router/guard"
import { flatMultiLevelRoutes } from "./helper"

const Layouts = () => import("@/layouts/index.vue")

/**
 * @name 常驻路由
 * @description 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置唯一的 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: ":path(.*)",
        component: () => import("@/pages/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/pages/error/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/pages/error/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/pages/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/",
    component: Layouts,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/pages/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          svgIcon: "dashboard",
          affix: true
        }
      }
    ]
  }
]

/**
 * @name 动态路由
 * @description 用来放置有权限 (Roles 属性) 的路由
 * @description 必须带有唯一的 Name 属性
 */
export const dynamicRoutes: RouteRecordRaw[] = [
  {
    // 学生端职位模块（动态路由，注入到统一布局）
    path: "/internship/position-student",
    component: Layouts,
    meta: { title: "实习管理", roles: ["STUDENT"] },
    name: "PositionStudentPane",
    redirect: "/internship/position-student/list",
    children: [
      {
        path: "list",
        name: "PositionStudentPaneList",
        component: () => import("@/pages/internship/position-student/index.vue"),
        meta: { title: "职位选择" }
      },
      {
        path: "apply",
        name: "PositionStudentPaneApply",
        component: () => import("@/pages/internship/application/appliction-student/index.vue"),
        meta: { title: "我的申请" }
      }
    ]
  },
  {
    path: "/internship/company",
    name: "CompanyPane",
    redirect: "/internship/company/list",
    component: Layouts,
    meta: { title: "公司管理", roles: ["TEACHER", "ADMIN"] },
    children: [
      {
        path: "list",
        name: "CompanyPaneList",
        component: () => import("@/pages/internship/company/index.vue"),
        meta: { title: "公司管理" }
      }
    ]
  },
  {
    path: "/internship/position-teacher",
    name: "PositionTeacherPane",
    redirect: "/internship/position-teacher/list",
    component: Layouts,
    meta: { title: "职位管理", roles: ["TEACHER", "ADMIN"] },
    children: [
      {
        path: "list",
        name: "PositionTeacherPaneList",
        component: () => import("@/pages/internship/position/position-teacher/index.vue"),
        meta: { title: "职位管理" }
      },
      {
        path: "review",
        name: "PositionTeacherPaneReview",
        component: () => import("@/pages/internship/application/application-teacher/index.vue"),
        meta: { title: "职位审核", hidden: true }
      }
    ]
  },
  {
    path: "/internship/resume",
    name: "ResumePane",
    redirect: "/internship/resume/list",
    component: Layouts,
    meta: { title: "个人简历", roles: ["STUDENT"] },
    children: [
      {
        path: "list",
        name: "ResumePaneList",
        component: () => import("@/pages/internship/resume/index.vue"),
        meta: { title: "简历列表" }
      },
      {
        path: "add",
        name: "ResumePaneAdd",
        component: () => import("@/pages/internship/resume/AddResume.vue"),
        meta: { title: "添加简历" }
      }
    ]
  }
]

/** 路由实例 */
export const router = createRouter({
  history: routerConfig.history,
  routes: routerConfig.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  try {
    // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
    router.getRoutes().forEach((route: any) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    location.reload()
  }
}

// 注册路由导航守卫
registerNavigationGuard(router)
